package com.zy.cat.common.utils;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;

import com.alibaba.fastjson.JSON;
import com.taobao.api.internal.util.WebUtils;
import com.zy.cat.entity.resp.ResultPage;
import com.zy.cat.entity.wy.CatShopCouponInfo;
import com.zy.cat.entity.wy.CatShopItemResp;
import com.zy.cat.entity.wy.TKConvertResp;
import lombok.extern.slf4j.Slf4j;

/**
 * 维易淘宝客API
 * 
 * @author Link
 * @date 2019-06-28 08:52
 */
@Slf4j
public class WeiYiAPI {

	//private String vekey = "V00003232Y24637560";
	private String vekey = "V00006186Y61299939";
	//private String pid = "mm_127127112_1057200437_109777600377";
	private String pid = "mm_329050115_2267150011_111382750040";
	private String url = "http://mvapi.vephp.com/";
	
//	private String appkey = "28121056";
//	private String secret = "6357fff5b950316c31498ef845aa9f2b";
	private String appkey = "32652006";
	private String secret = "57b3fbd679a1522e2e52c9b4aa2df9a9";

	private WeiYiAPI(String vekey, String pid, String url, String appkey, String secret) {
		super();
		this.vekey = vekey;
		this.pid = pid;
		this.url = url;
		this.appkey = appkey;
		this.secret = secret;
	}

	public static void main(String[] args) {

		/*
		 * String sid="605512131678",rid=""; TKConvertResp
		 * result=WeiYiAPI.getInstance(40).getTKConvert(sid, rid);
		 * log.info("--->"+JSON.toJSONString(result,true));  
		 */

		
		  String itemId="637182921051",sellerId="3538683476";
		CatShopCouponInfo result=WeiYiAPI.getInstance(20).getShopsCoupon(itemId,sellerId);
		  log.info("--->"+JSON.toJSONString(result,true));
		 
	}

	/**
	 * 
	 * @author Link
	 * @date 2019-06-28 08:53
	 * @param type 10.九趣猫安卓专用、11.九趣猫IOS专用、20.淘淘集安卓专用、21.淘淘集IOS专用、30.一陶安卓专用、31.一陶IOS专用、40.大姨妈/小说
	 * @return
	 */
	public static WeiYiAPI getInstance(int type) {
		String vekey = "V00002137Y48797019";
		String pid = "mm_124488621_35672161_109013650458";
		String url = "http://mvapi.vephp.com/";
		String appkey = "25650123";
		String secret = "1916f2cbd3354d56be0f822f349692c6";
		if (type == 10) {
			appkey = "28121056";
			secret = "6357fff5b950316c31498ef845aa9f2b";
			vekey = "V00003232Y24637560";
			pid = "mm_127127112_1057200437_109739900432";
		} else if (type == 11) {
			appkey = "28121056";
			secret = "6357fff5b950316c31498ef845aa9f2b";
			vekey = "V00003232Y24637560";
			pid = "mm_127127112_1057200437_109777600377";
		} else if (type == 20) {
			appkey = "29302482";
			secret = "4d90eaa3d2a165a31d57ead1fafa63ab";
			pid = "mm_127127112_1520400456_110383000089";
		} else if (type == 21) {
			appkey = "29302482";
			secret = "4d90eaa3d2a165a31d57ead1fafa63ab";
			pid = "mm_127127112_1520400456_110383000089";
		} else if (type == 30) {
			appkey = "29515689";
			secret = "e4059232968c98a1634651b93f5d0e45";
			pid = "mm_127127112_1663200385_110379750122";
		} else if (type == 31) {
			appkey = "29515689";
			secret = "e4059232968c98a1634651b93f5d0e45";
			pid = "mm_127127112_1663200385_110379750122";
		} else if (type == 40) {
//			appkey = "31342048";
//			secret = "3b914cfdcad24644d600b6379550383b";
//			pid = "mm_359360108_426650050_109083950159";
//			vekey = "V00002306Y59256055";
			appkey = "32652006";
			secret = "57b3fbd679a1522e2e52c9b4aa2df9a9";
			pid = "mm_329050115_2267150011_111382750040";
			vekey = "V00006186Y61299939";
		}
		return new WeiYiAPI(vekey, pid, url, appkey, secret);
	}

	/**
	 * 高佣转链
	 * 
	 * @author Link
	 * @date 2019-06-14 09:08
	 * @param para 商品ID、商品淘口令、优惠哦全等信息
	 * @param rid  渠道ID(relationId)
	 * @return
	 */
	public TKConvertResp getTKConvert(String para, String rid) {
		TKConvertResp tKConvertResp = null;
		Map<String, String> props = new HashMap<String, String>();
		props.put("vekey", vekey);
		props.put("relationId", rid);
//		props.put("sina", "1");
		props.put("pid", pid);
		props.put("para", para);
		props.put("detail", "1");
		props.put("sclickurl", "1");// 获取进入店铺链接
		props.put("appkey", appkey);
		props.put("secret", secret);
		
		/**
		 * para 支持一下类型 传递时需要urlencode （1）淘宝产品id：比如 557662422568 或直接产品链接
		 * //item.taobao.com/item.htm?id=...
		 * 
		 * （2）二合一链接：比如 https://uland.taobao.com/coupon/edetail?e=...
		 * 
		 * （3）s.click.taobao.com 长和短链接，比如 https://s.click.taobao.com/M9CMYLw
		 * 
		 * （4）天猫的喵口令 如 http://zmnxbc.com/s/YJk6Z
		 * 
		 * （5）手淘APP的分享链接：http://m.tb.cn/h.WDXp8bv
		 * 
		 * （6）别人的淘口令：如 ￥40pM0yyztET￥，或手淘分享链接文本直接传参数。
		 * 
		 * （7）新浪短网址，百度短网址封装的淘宝链接：http://t.cn/E6jP2DJ
		 * 
		 * （8）其它一些不常见的淘客链接
		 * 
		 * （9）手淘上分享的内容直接做参数传参数
		 */

		try {
			String json = WebUtils.doGet(url + "hcapi", props, "UTF-8");
			if (StringUtils.isNotBlank(json)) {
				log.info("-getTKConvert-->" + json);
				tKConvertResp = JSON.parseObject(json, TKConvertResp.class);
			}
		} catch (IOException e) {
			return new TKConvertResp(10, e.getLocalizedMessage());
		}
		tKConvertResp = tKConvertResp == null ? new TKConvertResp(404, "转链失败") : tKConvertResp;
		return tKConvertResp;
	}

	/**
	 * 淘宝天猫查询店铺优惠券
	 * 
	 * @author Link
	 * @date 2019-06-14 09:08
	 * @param para 商品ID、店铺ID全等信息
	 * @param rid  渠道ID(relationId)
	 * @return
	 */
	public CatShopCouponInfo getShopsCoupon(String itemId,String sellerId) {
		CatShopCouponInfo shopCouponInfo = null;
		Map<String, String> props = new HashMap<String, String>();
		props.put("vekey", vekey);
//		props.put("relationId", rid);
//		props.put("para", para);
		props.put("itemId", itemId);
		props.put("sellerId", sellerId);
		props.put("appkey", appkey);
		props.put("secret", secret);

		/**
		 * para 支持一下类型 传递时需要urlencode （1）淘宝产品id：比如 557662422568 或直接产品链接
		 * //item.taobao.com/item.htm?id=...
		 * 
		 * （2）二合一链接：比如 https://uland.taobao.com/coupon/edetail?e=...
		 * 
		 * （3）s.click.taobao.com 长和短链接，比如 https://s.click.taobao.com/M9CMYLw
		 * 
		 * （4）天猫的喵口令 如 http://zmnxbc.com/s/YJk6Z
		 * 
		 * （5）手淘APP的分享链接：http://m.tb.cn/h.WDXp8bv
		 * 
		 * （6）别人的淘口令：如 ￥40pM0yyztET￥，或手淘分享链接文本直接传参数。
		 * 
		 * （7）新浪短网址，百度短网址封装的淘宝链接：http://t.cn/E6jP2DJ
		 * 
		 * （8）其它一些不常见的淘客链接
		 * 
		 * （9）手淘上分享的内容直接做参数传参数
		 */

		try {
			String json = WebUtils.doGet(url + "shopcoupon", props, "UTF-8");
			if (StringUtils.isNotBlank(json)) {
				log.info("-getShopCoupon-->" + json);
				shopCouponInfo = JSON.parseObject(json, CatShopCouponInfo.class);
			}
		} catch (IOException e) {
			return new CatShopCouponInfo(20, e.getLocalizedMessage());
		}
		shopCouponInfo = shopCouponInfo == null ? new CatShopCouponInfo(404, "查询失败") : shopCouponInfo;
		return shopCouponInfo;
	}

	/**
	 * 淘宝店铺商品查询
	 * @param pageno
	 * @param userId
	 * @return
	 */
	public CatShopItemResp getShopItem(String pageno, String userId) {
		// TODO Auto-generated method stub
//		CatShopItemResp shopItemResp = null;
//		Map<String, String> props = new HashMap<String, String>();
//		props.put("vekey", vekey);
////		props.put("relationId", rid);
////		props.put("para", para);
//		props.put("pageno", pageno);
//		props.put("userId", userId);
//		props.put("appkey", appkey);
//		props.put("secret", secret);
//		
//		try {
//			String json = WebUtils.doGet(url + "shopitems", props, "UTF-8");
//			if (StringUtils.isNotBlank(json)) {
//				log.info("-getShopItem-->" + json);
//				shopItemResp = JSON.parseObject(json, CatShopItemResp.class);
//			}
//		} catch (IOException e) {
//			return new CatShopItemResp(20, e.getLocalizedMessage());
//		}
//		shopItemResp = shopItemResp == null ? new CatShopItemResp(404, "查询失败") : shopItemResp;
//		return shopItemResp;
		return null;
	}
}
